package com.sugar.subscribe.course.module.business.service.coachcertificate;

import cn.hutool.core.collection.CollUtil;
import com.sugar.subscribe.course.module.business.controller.app.coachcertificate.vo.AppCoachCertificateRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coachcertificate.vo.AppCoachCertificateSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.subscribe.course.module.business.controller.admin.coachcertificate.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachcertificate.CoachCertificateDO;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;

import com.sugar.subscribe.course.module.business.dal.mysql.coachcertificate.CoachCertificateMapper;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.*;

/**
 * 教练证书 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class CoachCertificateServiceImpl implements CoachCertificateService {

    @Resource
    private CoachCertificateMapper coachCertificateMapper;

    @Override
    public Long createCoachCertificate(CoachCertificateSaveReqVO createReqVO) {
        // 插入
        CoachCertificateDO coachCertificate = BeanUtils.toBean(createReqVO, CoachCertificateDO.class);
        coachCertificateMapper.insert(coachCertificate);

        // 返回
        return coachCertificate.getId();
    }

    @Override
    public void updateCoachCertificate(CoachCertificateSaveReqVO updateReqVO) {
        // 校验存在
        validateCoachCertificateExists(updateReqVO.getId());
        // 更新
        CoachCertificateDO updateObj = BeanUtils.toBean(updateReqVO, CoachCertificateDO.class);

        if (updateReqVO.getAuditStatus().equals(10))
            updateObj.setRefuseCause("");

        coachCertificateMapper.updateById(updateObj);
    }

    @Override
    public void deleteCoachCertificate(Long id) {
        // 校验存在
        validateCoachCertificateExists(id);
        // 删除
        coachCertificateMapper.deleteById(id);
    }

    @Override
        public void deleteCoachCertificateListByIds(List<Long> ids) {
        // 删除
        coachCertificateMapper.deleteByIds(ids);
        }


    private void validateCoachCertificateExists(Long id) {
        if (coachCertificateMapper.selectById(id) == null) {
            throw exception(COACH_CERTIFICATE_NOT_EXISTS);
        }
    }

    @Override
    public CoachCertificateDO getCoachCertificate(Long id) {
        return coachCertificateMapper.selectById(id);
    }

    @Override
    public PageResult<CoachCertificateRespVO> getCoachCertificatePage(CoachCertificatePageReqVO pageReqVO) {
        return coachCertificateMapper.selectPage(pageReqVO);
    }

    @Override
    public Long appCreateCoachCertificate(AppCoachCertificateSaveReqVO createReqVO, Long coachId) {
        // 插入
        CoachCertificateDO coachCertificate = BeanUtils.toBean(createReqVO, CoachCertificateDO.class);
        coachCertificate.setCoachId(coachId).setAuditStatus(0);
        coachCertificateMapper.insert(coachCertificate);
        return coachCertificate.getId();
    }

    @Override
    public PageResult<AppCoachCertificateRespVO> appGetCoachCertificatePage(PageParam pageParam, Long coachId) {
        return BeanUtils.toBean(coachCertificateMapper.selectPage(
                BeanUtils.toBean(
                        pageParam, CoachCertificatePageReqVO.class).setCoachId(coachId)),
                AppCoachCertificateRespVO.class);
    }
}